from pyrfid.DataProtocol.CommandResponseUnit.CommandResponseUnit import CommandResponseUnit
from pyrfid.Util.Util import Util
from pyrfid.DataProtocol.Driver.DriverInterface import DriverInterface

from pyrfid.ApplicationInterface.ApplicationCommand import ApplicationCommand
from pyrfid.ApplicationInterface.argument.DSFID import DSFID
from pyrfid.ApplicationInterface.argument.ApplicationFamilyId import ApplicationFamilyId
from pyrfid.ApplicationInterface.argument.AddObjects import AddObjects
from pyrfid.DataProtocol.Driver.JTEUSBDriver import JTEUSBDriver
from pyrfid.DataProtocol.Driver.UHFDriver import UHFDriver

from pyrfid.DataProtocol.CommandResponseUnit.CommandResponseUnit import CommandResponseUnit
from pyrfid.Util.Util import Util
from pyrfid.DataProtocol.Driver.DriverInterface import DriverInterface

from pyrfid.ApplicationInterface.ApplicationCommand import ApplicationCommand
from pyrfid.ApplicationInterface.argument.DSFID import DSFID
from pyrfid.ApplicationInterface.argument.ApplicationFamilyId import ApplicationFamilyId
from pyrfid.ApplicationInterface.argument.AddObjects import AddObjects
from pyrfid.DataProtocol.Driver.JTEUSBDriver import JTEUSBDriver
from pyrfid.DataProtocol.Driver.UHFDriver import UHFDriver
from pyrfid.ApplicationInterface.ReadObjectsCommand import ReadObjectsCommand
from pyrfid.ApplicationInterface.EraseMemoryCommand import EraseMemoryCommand
from pyrfid.DataProtocol.Driver.VirtualUHFDriver import VirtualUHFDriver
from pyrfid.ApplicationInterface.ConfigureAFICommand import ConfigureAFICommand
from pyrfid.ApplicationInterface.WriteObjectsSegmentedMemoryTagCommand import WriteObjectsSegmentedMemoryTagCommand

if __name__ == '__main__':
    
    
    outputString = ""
    dsfid = DSFID(0, 9)
    afi = ApplicationFamilyId(1, 1)
    accessPassword = "hellow world"
    memoryBank = "2"
    objectIdentifier = "1 0 15961 9 1" 
    avoidDuplicate = True
    objectValue = "19"
    compactParameter = 1
    objectLock = True
    addObjectsList = []
    addObjectsList.append(AddObjects(objectIdentifier, avoidDuplicate, objectValue, compactParameter, objectLock))
    
    objectIdentifier2 = "1 0 15961 9 2"    
    avoidDuplicate = True
    objectValue = "1234567"
    compactParameter = 1
    objectLock = True
    addObjectsList.append(AddObjects(objectIdentifier2, avoidDuplicate, objectValue, compactParameter, objectLock))
    
    objectIdentifier2 = "1 0 15961 9 7003 2"    
    avoidDuplicate = True
    objectValue = "0901041007"    
#    addObjectsList.append(AddObjects(objectIdentifier2, avoidDuplicate, objectValue, compactParameter, objectLock))

    objectIdentifier2 = "1 0 15961 9 7003 3"    
    avoidDuplicate = True
    objectValue = "1003091322"    
#    addObjectsList.append(AddObjects(objectIdentifier2, avoidDuplicate, objectValue, compactParameter, objectLock))    

    objectIdentifier2 = "1 0 15961 9 7003 3"    
    avoidDuplicate = True
    objectValue = "1011051429"    
#    addObjectsList.append(AddObjects(objectIdentifier2, avoidDuplicate, objectValue, compactParameter, objectLock))    
    
    objectIdentifier3 = "1 0 15961 9 3912"    
    avoidDuplicate = True
    objectValue = "978123456"
    compactParameter = 1
    objectLock = True
#    addObjectsList.append(AddObjects(objectIfdentifier3, avoidDuplicate, objectValue, compactParameter, objectLock))
    
    tagId = "E200C0D2B1784380"

#    driver = JTEUSBDriver(UHFDriver.LEVEL_0)
#    driver.connectRW("/dev/tty.SLAB_USBtoUART")
    
#    applicationCommand = ConfigureAFICommand(tagId, afi, False)
#    applicationEncodedCommand = applicationCommand.encode()
#    cru = CommandResponseUnit(applicationEncodedCommand, driver)
#    cru.execute()
#    sys.exit()
    
    applicationCommand = ApplicationCommand.generateCommand(ApplicationCommand.WRITE_OBJECTS_SEGMENTED_MEMORY_TAG, tagId=tagId, memoryBank=memoryBank, accessPassword=accessPassword, AFI = afi, DSFID=dsfid, addObjectsList=addObjectsList)
#    
#    
#Read Command
#    applicationCommand = "060528FC597E1130110410453230304330443242313738343338300201023000"

#Write Command
    #Packed Objects
    applicationCommand = applicationCommand.encode()

    #applicationCommand = "060528FC597E12304604104532303043304432423137383433383004023131040870617373776F7264020101020102020102020108302B060728FC5909832501010100040D34393131313131303030303134020101010100060728FC5909B65B03010100040A30393031303131323130020101010100"
    
    #Directory
#    applicationCommand = "060528FC597E12304604104532303043304432423137383433383004023131040870617373776F7264020101020102020101020108302B060728FC5909832501010100040D34393131313131303030303134020101010100060728FC5909B65B03010100040A30393031303131323130020101010100"
    
    #Tag-Data-Profile
#    applicationCommand = "060528FC597E12304604104532303043304432423137383433383004023131040870617373776F7264020101020102020103020108302B060728FC5909832501010100040D34393131313131303030303134020101010100060728FC5909B65B03010100040A30393031303131323130020101010100"


    #Non-Directory
#    applicationCommand = "060528FC597E12304F04104532303043304432423137383433383004023131040870617373776F72640201010201020201000201013034060628FC59098633010100041D554E3034333332353731314D4838303331323030303030303030303031020101010100060528FC5909020101000406313130373830020101010100"


#    driver = JTEUSBDriver(UHFDriver.LEVEL_0)
#    driver.connectRW("/dev/tty.SLAB_USBtoUART")
    
    driver = VirtualUHFDriver(True)
    driver.initializeVirtualTag("0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")

    cru = CommandResponseUnit(applicationCommand, driver)
    cru.execute()
#    sys.exit()
#    erase_command = EraseMemoryCommand(tagId)
#    cru = CommandResponseUnit(erase_command.encode(), driver)
#    response_command = cru.execute()
#    sys.exit()
    
    read_object_command = ReadObjectsCommand(tagId, ReadObjectsCommand.READ_ALL_OBJECTS)
    cru = CommandResponseUnit(read_object_command.encode(), driver)
    response_command = cru.execute()
    print read_object_command.decode(response_command)
    